package app.foo.planetadnzombiedemo.global;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Build;
import android.util.DisplayMetrics;

import androidx.annotation.RequiresApi;

/**
 * 处理设备适应性
 */
public class DeviceHelper {
    private static int[]deviceSize=new int[2];

    /**
     * 对位图进行缩放处理
     * @param bitmap
     * @return
     */
    public static Bitmap resizeBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            //进行变换，矩阵变换
            Matrix matrix = new Matrix();
            matrix.postScale(Config.scaleWidth, Config.scaleHeight);
            Bitmap reszieBitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
            return reszieBitmap;
        } else {
            return null;
        }
    }

    public static Bitmap resizeBitmap(Bitmap bitmap,int w,int h) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth=w;
            int newHeight=h;

            float scaleWidth=((float)newWidth)/width;
            float scaleHeight=((float)newHeight)/height;

            //进行变换，矩阵变换
            Matrix matrix = new Matrix();
            matrix.postScale(Config.scaleWidth, Config.scaleHeight);
            Bitmap reszieBitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
            return reszieBitmap;
        } else {
            return null;
        }
    }
    /**
     * 获取设备尺寸
     * @param context
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static int[] getDeviceInfo(Context context){
        if(deviceSize[0]==0&&deviceSize[1]==0){
            DisplayMetrics metrics=new DisplayMetrics();
            ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
            deviceSize[0]=metrics.widthPixels;
            deviceSize[1]=metrics.heightPixels;
        }
        return deviceSize;

    }
}
